﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.SystemModel
{
    public partial class Department : Aliyeye.Car.Net.UI.BaseForm
    {
        Base_DepartmentSvc.SoapHeaderClass sopaHeader = new Base_DepartmentSvc.SoapHeaderClass();
        private String strID = "";

        public Department()
        {
            InitializeComponent();
        }

        private void Department_Load(object sender, EventArgs e)
        {
            this.pager1.PageCurrent = 1;
            this.pager1.Bind();
        }

        private int dgvBind()
        {
            string _strSql = "";
            string strWhere = "";
            PageData pageData = new PageData(this, pager1, overlay);
            pageData.TableName = "Base_Department";
            pageData.PrimaryKey = "ClassID";
            pageData.OrderStr = "ClassID desc";
            pageData.PageIndex = this.pager1.PageCurrent;
            pageData.PageSize = this.pager1.PageSize;
            pageData.QueryCondition = _strSql + strWhere.ToString();
            pageData.QueryFieldName = "*";

            DataSet ds = pageData.QueryDataTable();
            dgvInfo.AutoGenerateColumns = false;
            dgvInfo.DataSource = this.pager1.bindingSource;
            return pageData.TotalCount;
        }

        private int pager1_EventPaging(EventPagingArg e)
        {
            return dgvBind();
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        //添加分店部门
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRoleName.Text))
            {
                DialogWarning dia = new DialogWarning("分店部门名称不能为空！");
                dia.ShowDialog();
                return;
            }

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;

            Base_DepartmentSvc.Base_Department model = new Base_DepartmentSvc.Base_Department();
            model.ClassName = txtRoleName.Text;
            //BLL.Base_Department bll = new BLL.Base_Department();

            //添加
            if (strID == "")
            {
                ServiceManipulator.SafeAsyncExecution<Base_DepartmentSvc.Base_DepartmentServiceSoapClient>(
                    proxy => proxy.Add(sopaHeader, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("添加分店部门成功！");
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                            btnAdd.Text = "添加";
                            txtRoleName.Text = "";
                            strID = "";
                            this.pager1.Bind();
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
            else
            {
                //修改
                model.ClassID = Convert.ToInt32(strID);

                ServiceManipulator.SafeAsyncExecution<Base_DepartmentSvc.Base_DepartmentServiceSoapClient>(
                    proxy => proxy.Update(sopaHeader, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("修改分店部门成功！");
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                            btnAdd.Text = "添加";
                            txtRoleName.Text = "";
                            strID = "";
                            this.pager1.Bind();
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                ButtonClicked(e);
            }
        }

        private void ButtonClicked(DataGridViewCellEventArgs e)
        {
            String strButtonContent = dgvInfo.Columns[e.ColumnIndex].Name;
            String id = dgvInfo.Rows[e.RowIndex].Cells["编号"].Value.ToString();
            String name = dgvInfo.Rows[e.RowIndex].Cells["分店部门名称"].Value.ToString();
            if (strButtonContent.Trim().Equals("修改"))
            {
                this.txtRoleName.Text = name;
                strID = id;
                btnAdd.Text = "保存";
                btnAdd.Refresh();
                btnCancel.Enabled = true;
            }
            else if (strButtonContent.Trim().Equals("删除"))
            {
                if (dgvInfo.RowCount <= 1)
                {
                    PageCommon.Alert("分店部门必须保留一个，或请修改此分店部门！");
                    return;
                }
                //"确定删除？"
                DialogConfirm diaSecc = new DialogConfirm("确定删除此分店部门吗？");
                if (diaSecc.ShowDialog() == DialogResult.OK)
                {
                    overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
                   int intID= Convert.ToInt32(id);

                    ServiceManipulator.SafeAsyncExecution<Base_DepartmentSvc.Base_DepartmentServiceSoapClient>(
                    proxy => proxy.Delete(sopaHeader, intID),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("删除分店部门成功！");
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                            btnAdd.Text = "添加";
                            btnAdd.Refresh();
                            txtRoleName.Text = "";
                            strID = "";
                            this.pager1.Bind();
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
                }
            }
        }

        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (dgvInfo.Columns[cellEvent.ColumnIndex] is
                 DataGridViewButtonColumn &&
                 cellEvent.RowIndex != -1)
            { return true; }
            else { return (false); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnAdd.Text = "添加";
            btnAdd.Refresh();
            txtRoleName.Text = "";
            strID = "";
            btnCancel.Enabled = false;
        }

    }
}
